package cn.melonlib.graphql.tools.comp.service;


import cn.melonlib.graphql.annos.GraphqlParameter;
import cn.melonlib.graphql.annos.GraphqlQuery;
import cn.melonlib.graphql.annos.GraphqlSchema;
import cn.melonlib.graphql.cores.comp.utils.GraphqlScanner;
import cn.melonlib.graphql.enums.GraphqlSchemaPrefixMode;
import cn.melonlib.graphql.model.metadata.GraphqlFunction;
import cn.melonlib.graphql.model.metadata.GraphqlView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@GraphqlSchema(namespace = "GraphqlMetadata",comment = "查看元数据信息",value = GraphqlSchemaPrefixMode.NONE)
@Service
public class GraphqlMetadataService {

    @Resource
    private GraphqlScanner graphqlScanner;

    @GraphqlQuery(comment = "函数信息")
    public List<GraphqlFunction> functions(@GraphqlParameter(value = "namespace",comment = "命名空间，如果为空获得默认命名空间") String namespace){
        return graphqlScanner.getFunctions().get(StringUtils.defaultIfBlank(namespace,graphqlScanner.defaultNamespace)).entrySet().stream().map(Map.Entry::getValue).toList();
    }

    @GraphqlQuery(comment = "类型信息")
    public List<GraphqlView> views(@GraphqlParameter(value = "namespace",comment = "命名空间，如果为空获得默认命名空间") String namespace){
        return  graphqlScanner.getViewTypes().get(StringUtils.defaultIfBlank(namespace,graphqlScanner.getDefaultNamespace()))
                .entrySet().stream().map(Map.Entry::getValue).toList();
    }

}
